package com.xbn.xbnmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xbn.xbnmall.product.dao.BrandDao;
import com.xbn.xbnmall.product.dao.CategoryDao;
import com.xbn.xbnmall.product.entity.BrandEntity;
import com.xbn.xbnmall.product.entity.CategoryEntity;
import com.xbn.xbnmall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.Query;

import com.xbn.xbnmall.product.dao.CategoryBrandRelationDao;
import com.xbn.xbnmall.product.entity.CategoryBrandRelationEntity;
import com.xbn.xbnmall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
   private BrandDao brandDao;
    @Autowired
   private CategoryDao categoryDao;
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询品牌名字和分类名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }
    /*
    * 更改指定brandId的数据的brandName
    * */
    @Override
    public void updateBrandName(Long brandId, String name) {
    CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        //更新条件：品牌id为指定值的
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>()
       .eq("brand_id",brandId)
        );
    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(name);
        //更新条件：菜单分类id为指定值的
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id",catId)
        );
    }

    /*
    * 查询指定分类下的所有品牌的信息
    * */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationDao.selectList(wrapper);
        List<BrandEntity> brandEntities=new ArrayList<>();
        if(categoryBrandRelationEntities!=null&&categoryBrandRelationEntities.size()>0){
            List<Long> brandIds = categoryBrandRelationEntities.stream().map((item) -> {
                return item.getBrandId();
            }).collect(Collectors.toList());
            brandEntities = brandDao.selectBatchIds(brandIds);
        }
        return brandEntities;
    }

}